﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;

namespace wpControl1
{
    /// <summary>
    /// Manages the applications start mode.
    /// </summary>
    class StartManager
    {
        /// <summary>
        /// Stores the start mode setting in the registry, so that the app is 
        /// executed on startup if requested by user.
        /// </summary>
        internal void StoreStartMode()
        {
            // Open the registry key responsible for windows autostart
            RegistryKey startKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            // App should start automatically
            if (Program.SettingsManager.StartWithWindows)
            {
                // Write key
                startKey.SetValue("WallpaperControl", Application.ExecutablePath.ToString());
            }
            // App should not start automatically
            else
            {
                // Delete key
                startKey.DeleteValue("WallpaperControl", false);
            }
        }

        /// <summary>
        /// Gets the applications start mode.
        /// </summary>
        /// <returns>True if application currently start automatically, false otherwise.</returns>
        internal bool GetStartMode()
        {
            bool autostart;
            RegistryKey startKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", false);
            if (startKey.GetValue("WallpaperControl") == null)
            {
                autostart = false;
            }
            else
            {
                autostart = true;
            }
            return autostart;
        }
    }
}
